<template>
  <!--系统通告-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="50">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <div style="width: 60px">标题:</div>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              style="margin-right: 10px"
              placeholder="请输入标题"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search()">
              查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-refresh-right"
              @click="restart"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
      <vab-query-form-left-panel :span="8">
        <el-button type="primary" icon="el-icon-plus" @click="add()">
          新增
        </el-button>
        <el-button type="primary" icon="el-icon-download" @click="handleExport">
          导出
        </el-button>
        <el-button type="primary" icon="el-icon-upload2">导入</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="titile"
        label="标题"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="sender"
        label="发布人"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="msg_type"
        label="通告对象"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="send_status"
        label="发布状态"
      >
        <template #default="{ row }">
          {{
            row.send_status == 1
              ? '已发布'
              : row.send_status == 2
              ? '未发布'
              : ''
          }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="send_time"
        label="发布时间"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="cancel_time"
        label="撤销时间"
      />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-dropdown style="color: #1890ff">
            <span class="el-dropdown-link">
              更多
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="backList(scope.row)">
                撤销
              </el-dropdown-item>
              <el-dropdown-item @click.native="edit(scope)">
                查看
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="标题:" required>
          <el-input v-model="form.titile" />
        </el-form-item>
        <el-form-item label="通告类型:" required>
          <el-select
            v-model="form.msg_type"
            clearable
            placeholder="请选择通告类型"
            @change="selectType()"
          >
            <el-option
              v-for="item in type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间:" required>
          <el-date-picker
            v-model="form.start_time"
            type="date"
            placeholder="选择开始时间"
          />
        </el-form-item>
        <el-form-item label="结束时间:" required>
          <el-date-picker
            v-model="form.end_time"
            type="date"
            placeholder="选择结束时间"
          />
        </el-form-item>
        <quill-editor
          ref="text"
          v-model="form.msg_content"
          class="myQuillEditor"
          :options="editorOption"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-show="addtype" type="primary" @click="send()">
          确 定
        </el-button>
        <el-button v-show="edittype" type="primary" @click="save()">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
  import {
    getTaxSystemMsgLists,
    addTaxSystemMsgs,
    editTaxSystemMsgs,
    editTaxSystemMsgDelFlags,
    editTaxSystemMsgSendStatuss,
  } from '@/api/common'
  import { quillEditor } from 'vue-quill-editor'
  import 'quill/dist/quill.core.css'
  import 'quill/dist/quill.snow.css'
  import 'quill/dist/quill.bubble.css'

  export default {
    name: 'Notice',
    components: { quillEditor },
    data() {
      return {
        queryForm: {
          title: '',
        },
        form: {
          titile: '',
          msg_type: '',
          start_time: '',
          end_time: '',
          msg_content: '',
        },
        editorOption: '',
        disabled: false,
        listLoading: false,
        list: [
          {
            title: '名称',
            user: '张三',
            to: '李四',
            statu: 1,
            create_time: '2021-01-01 12:01',
            back_time: '2021-01-08 12:01',
          },
        ],
        type: [
          {
            value: 1,
            label: '活动',
          },
          {
            value: 2,
            label: '地产',
          },
        ],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogFormVisible: false,
        wintit: '',
        noticeId: '',
        addtype: false,
        edittype: false,
      }
    },
    created() {
      this.getData()
    },
    methods: {
      //搜索
      search() {
        this.getData()
      },
      restart() {
        this.queryForm = []
        this.getData()
      },
      //导出
      handleExport() {},
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          title: this.queryForm.title,
        }
        getTaxSystemMsgLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      add() {
        this.dialogFormVisible = true
        this.wintit = '添加'
        this.addtype = true
        this.edittype = false
        this.form = []
      },
      edit(row) {
        this.dialogFormVisible = true
        this.wintit = '编辑'
        this.addtype = false
        this.edittype = true
        this.form = this.list[row.$index]
        this.noticeId = row.row.id
      },
      send() {
        let params = {
          titile: this.form.titile,
          msg_content: this.form.msg_content,
          start_time: this.form.start_time,
          end_time: this.form.end_time,
          msg_type: this.form.msg_type,
        }
        addTaxSystemMsgs(params).then((data) => {
          this.$message.success('添加成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      save() {
        let params = {
          id: this.noticeId,
          titile: this.form.titile,
          msg_content: this.form.msg_content,
          start_time: this.form.start_time,
          end_time: this.form.end_time,
          msg_type: this.form.msg_type,
        }
        editTaxSystemMsgs(params).then((data) => {
          this.$message.success('修改成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      backList(row) {
        let params = {
          id: row.id,
          send_status: 2,
        }
        editTaxSystemMsgSendStatuss(params).then((data) => {
          this.$message.success('撤销成功！')
          this.getData()
        })
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
    margin-right: 20px;
  }
</style>
